package com.bsit.obu.base;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.FragmentActivity;
import android.view.WindowManager;
import android.widget.TextView;

import com.bsit.obu.R;
import com.bsit.obu.dialog.CommonToastDialog;
import com.bsit.obu.dialog.LoadingDialog;
import com.bsit.obu.utils.ActivityTask;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 界面基类
 * Created by DELL on 2016/5/18.
 */
public abstract class BaseActivity extends FragmentActivity {

    @BindView(R.id.title_tv)
    public TextView titleTv;
//    @BindView(R.id.right_tv)
//    public TextView rightTv;
    @BindView(R.id.bac_tv)
    public TextView bac_tv;
    private LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        initView();
        ButterKnife.bind(this);
        ActivityTask.getInstance().addAty(this);
        fillView();

    }


    protected abstract void initView();

    protected void fillView(){
        //rightTv.setVisibility(View.GONE);
    }

    @OnClick(R.id.bac_tv)
    void back(){
        finish();
    }

    /**
     * 自定handler类 避免handler内存回收问题导致的内存泄漏的问题
     */
    public static class StaticHandler extends Handler {
        WeakReference<Activity> mActivityReference;
        public MyHandlerMessageInterface handlerInterface;
        public StaticHandler(Activity activity, MyHandlerMessageInterface hanlerInterface) {
            mActivityReference = new WeakReference<>(activity);
            this.handlerInterface = hanlerInterface;
        }

        @Override
        public void handleMessage(Message msg) {
            final Activity activity = mActivityReference.get();
            if (activity != null) {
                this.handlerInterface.onHandlerMessage(msg);
            }
        }
    }
    public interface MyHandlerMessageInterface{
        void onHandlerMessage(Message msg);
    }

    public void initTitle(int titleRid){
        titleTv.setText(getString(titleRid));
    }

    public void notifyUser() {
        try {
            new MakeSomeNoise(getAssets().openFd("notify.mp3")
                    .getFileDescriptor()).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static class MakeSomeNoise extends Thread {

        private FileDescriptor fileDescriptor;

        public MakeSomeNoise(FileDescriptor fileDescriptor) {
            this.fileDescriptor = fileDescriptor;
        }

        @Override
        public void run() {
            try {
                MediaPlayer player = new MediaPlayer();
                player.setDataSource(fileDescriptor);
                player.prepare();
                player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.reset();
                        mp.release();
                    }
                });
                player.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 显示对话框
     *
     * @param message 弹出的消息
     */
    public void showDialog(String message) {
        dialog = new LoadingDialog(this, message);
        dialog.show();
    }

    /**
     * 完成取消对话框
     */
    public void dismissDialog() {
        if (dialog != null ) {
            dialog.dismiss();
        }
    }
    public CommonToastDialog commonToastDialog;
    public void showCommonDialog(String content){
        commonToastDialog =   new CommonToastDialog(this, "提示", content, new CommonToastDialog.InvoiceTypeListener() {
            @Override
            public void cancel() {

            }

            @Override
            public void confrim() {

            }
        });
        // commonToastDialog.updateContnet(content);
        if(!commonToastDialog.isShowing()){
            commonToastDialog.show();
        }

    }
}
